package com.demo.cloud.configuration;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class ZuulPreFilter extends ZuulFilter {

    /**
     * 返回过滤器的类型
     * pre
     * post
     * routing
     * error
     * @return 类型值
     */
    @Override
    public String filterType() {
        return "pre";
    }

    /**
     * 返回执行顺序，值越小越早执行
     * @return order
     */
    @Override
    public int filterOrder() {
        return 0;
    }

    /**
     * 是否执行该过滤器逻辑
     * true 执行run方法，false：不执行run方法
     * @return
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**
     * 过滤执行的逻辑
     * @return 是否执行成功
     */
    @Override
    public Object run() throws ZuulException {

        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();
        String uri = request.getRequestURI();
        StringBuffer url = request.getRequestURL();
        System.out.println("uri = " + uri);
        System.out.println("url=" + url);
        return null;
    }
}
